package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    private MemberService memberService;
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        //获得当前日期之前12个月的日期
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);//获取一年的  也就是 现在是2021。08  2020.08
        List<String> list =new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            //月份从0开始 显示加1
            calendar.add(Calendar.MONTH,1);
            //返回日历的当前的时间  添加进去的都是  2021-10
            list.add(new SimpleDateFormat("yyyy.MM").format(calendar.getTime()));
        }
        Map<String,Object> map =new HashMap<>();
        map.put("months",list);
        //查询每月的会员数量
        List<Integer> memberCount=memberService.findMemberCountByMonth(list);
        map.put("memberCount",memberCount);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }
}
